﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLySuKien.model;
using System.Data;

namespace QuanLySuKien
{
    public partial class TaoSuKien : System.Web.UI.Page
    {
        model.SuKienModel skmodel = new model.SuKienModel();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                ////load loai sk
                DropDownList1.DataSource = skmodel.DanhSachLoaiSK();
                DropDownList1.DataValueField = "MaLoaiSK";
                DropDownList1.DataTextField = "TenLoai";
                DropDownList1.DataBind();

                //load dia diem
                ListView_DiaDiem.DataSource = skmodel.DanhSachDiaDiemKhaDung();
                ListView_DiaDiem.DataBind();

                //load thiet bi
                ListView_NhanSu.DataSource = skmodel.DanhSachNhanSuKhaDung();
                ListView_NhanSu.DataBind();

                //load nhan su
                ListView_ThietBi.DataSource = skmodel.DanhSachThietBiKhaDung();
                ListView_ThietBi.DataBind();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            try
            {
                //luu vao session

                Dictionary<string, int> thietbi = new Dictionary<string, int>();
                Dictionary<string, int> nhansu = new Dictionary<string, int>();

                Session["thietbi"] = thietbi;
                Session["nhansu"] = nhansu;


                //Users user = new Users();

                //string tenSK = txtTenSK.Value;
                //string loaiSK = DropDownList1.SelectedValue;
                ////test
                ////string maKH = user.GetUserIdByUsername(User.Identity.Name);
                ////string maNV = user.GetUserIdByUsername(User.Identity.Name);
                //string maKH = "b59a15fe-108b-440d-b6c5-2e7ebabc0895";
                //string maNV = "b59a15fe-108b-440d-b6c5-2e7ebabc0895";
                //string ngaybatdau = Request.Form["ngay_bd"];
                //string ngayketthuc = Request.Form["ngay_kt"];
                //string mota = txtMota.Value;
                //string maDD = "";


                //tai du lieu vao xac nhan
                lbTenSK.Text = txtTenSK.Value;
                lbLoaiSK.Text = DropDownList1.SelectedItem.Text;
                lbNgayBD.Text = Request.Form["ngay_bd"];
                lbNgayKT.Text = Request.Form["ngay_kt"];
                lbMota.Text = txtMota.Value;
                Session["maDD"] = Request.Form["rddiadiem"];


                //insert danh sach thiet bi
                for (int i = 0; i < ListView_ThietBi.Items.Count; i++)
                {

                    if (!((TextBox)ListView_ThietBi.Items[i].FindControl("txtSLThietBi")).Text.Equals("0"))
                    {
                        int soluong = Int32.Parse(((TextBox)ListView_ThietBi.Items[i].FindControl("txtSLThietBi")).Text);
                        ((Dictionary<string, int>)Session["thietbi"]).Add(((Label)ListView_ThietBi.Items[i].FindControl("lbThietBi")).Text, soluong);
                    }

                }

                //insert danh sach nhan su
                for (int i = 0; i < ListView_NhanSu.Items.Count; i++)
                {

                    if (!((TextBox)ListView_NhanSu.Items[i].FindControl("txtSLNhanSu")).Text.Equals("0"))
                    {
                        int soluong = Int32.Parse(((TextBox)ListView_NhanSu.Items[i].FindControl("txtSLNhanSu")).Text);
                        ((Dictionary<string, int>)Session["nhansu"]).Add(((Label)ListView_NhanSu.Items[i].FindControl("lbNhanSu")).Text, soluong);
                    }

                }

                //int tonggia_thietbi = 

                MultiView_Checkout.SetActiveView(Payment);

                //dia diem da dc chon
                LV_diadiemXN.DataSource = skmodel.DiaDiemByMaDD(Session["maDD"].ToString());
                LV_diadiemXN.DataBind();

                //thiet bi da dc chon
                LV_thietbiXN.DataSource = (Dictionary<string, int>)Session["thietbi"];
                LV_thietbiXN.DataBind();

                //nhan su da dc chon
                LV_nhansuXN.DataSource = (Dictionary<string, int>)Session["nhansu"];
                LV_nhansuXN.DataBind();

                decimal tonggiathietbi = 0;
                //ting tong gia thiet bi
                for (int i = 0; i < LV_thietbiXN.Items.Count; i++)
                {
                    decimal gia = decimal.Parse(((Label)LV_thietbiXN.Items[i].FindControl("lbGiaTbDaChon")).Text);
                    int soluong = Int32.Parse(((Label)LV_thietbiXN.Items[i].FindControl("lbMaThietbiDaChon")).Text);
                    tonggiathietbi += gia * soluong;
                }

                lbTongGiaTB.Text = tonggiathietbi.ToString();


                decimal tonggianhansu = 0;
                //ting tong gia nhan su
                for (int i = 0; i < LV_nhansuXN.Items.Count; i++)
                {
                    decimal gia = decimal.Parse(((Label)LV_nhansuXN.Items[i].FindControl("lbGiaNhanSuDaChon")).Text);
                    int soluong = Int32.Parse(((Label)LV_nhansuXN.Items[i].FindControl("lbNhanSuDaChon")).Text);
                    tonggianhansu += gia * soluong;
                }

                lbTongTienNhanSu.Text = tonggianhansu.ToString();

                //tong chi phi
                decimal giaDD = decimal.Parse(skmodel.DiaDiemByMaDD(Session["maDD"].ToString()).Rows[0]["Gia"].ToString());
                lbTongChiPhi.Text = (tonggiathietbi + tonggianhansu + giaDD).ToString();

            }
            catch
            { 
            
            }


        }

        //thiet bi da chon
        public DataTable ThietBiByMaTB(string maTB)
        {
            return skmodel.ThietBiByMaTB(maTB);
        }

        //nhan su da chon
        public DataTable NhanSuByMaNS(string maNS)
        {
            return skmodel.NhanSuByMaNS(maNS);
        }


        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            MultiView_Checkout.SetActiveView(Shipping);
        }

        protected void btnXacNhan_Click(object sender, EventArgs e)
        {

        }




    }
}